package com.example.controller;

import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Employee;
import com.example.exception.CustomException;
import com.example.service.AdminService;
import com.example.service.EmployeeService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class WebController {
    @Resource
    private EmployeeService employeeService;
    @Resource
    private AdminService adminService;

    @GetMapping("/hello")
    public String hello(){
        return "hello,world";
    }

    //员工或管理员登录，现在两种人登录，所以用account类
    @PostMapping("/login")
    public Result login(@RequestBody Account account)
    {
        Account result =null;
        if("ADMIN".equals(account.getRole())){ //管理员登录
           result = adminService.login(account);
        }else if("EMP".equals(account.getRole())){
            result= employeeService.login(account);
    }
        return Result.success(result);
    }

    //员工注册
    @PostMapping("/register")
    public Result register(@RequestBody Employee employee) //注册不需要返回用户信息
    {
        employeeService.register(employee);
        return Result.success();
    }

//    @GetMapping("/weather")
//    public Result weather(){
//        return Result.success("今天天气晴朗");//为什么要用result包裹？——这样可以统一格式，方便查看
//    }
//
//    @GetMapping("/count")
//    public Result count(){ //检测出错误
//        int a=1/0;
//        return Result.success(10);
//    }
//
//    @GetMapping("/count1")
//    public Result count1(){//不是错误，是我主动不让别人访问设置的错误
//        throw new CustomException("400","错误！禁止请求！") ;
//       // throw new RuntimeException("错误！禁止请求！");
//    }
//
//    @GetMapping("/map")
//    public Result map() { //最终返回的是一个json格式的数据
//        HashMap<String,Object> map=new HashMap<>();
//        map.put("name","好处");
//        map.put("age",32);
//        return Result.success(map);
//    }
}
